package com.aier.admin.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.MD5Util;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.service.IAdminUserService;
import com.aier.common.common.CityUtil;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.entity.Proxy;
import com.aier.proxy.entity.ProxyCity;
import com.aier.proxy.service.IProxyService;
import com.aier.user.common.UserFinalPageSize;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("adminProxyCreateAction")
public class AdminProxyCreateAction extends AdminBaseAction {
    private static final long serialVersionUID = 1L;
    
    //页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    //错误码
    private int s;
    
    // 排序方式
    private String sord;
    // 排序字段
    private String sort;

    private List<Proxy> proxies;
    private List<ProxyCity> proxyc;
    private User user;
    private Map<Integer,String> cmap;

    private Long uid;
    // 代理商帐号
    private String account;
    private Integer cid;
    // 代理商名称
    private String proxyName;
    // 代理商邮箱
    private String email;
    // 代理商电话
    private String phone;
    
    private Proxy proxy;
    // 代理商id
    private Integer pid;
    
    private String cidStr;

    @Resource(name = "adminUserService")
    private IAdminUserService adminUserService;
    @Resource(name = "proxyService")
    private IProxyService proxyService;
    @Resource(name = "userService") 
    private IUserService userService;

    @Override
    public String execute() {
        return SUCCESS;
    }

    /**
     * 根据城市id检查是否有代理城市
     * 
     * @return
     */
    public String isExistProxyCity() {
        user = adminUserService.searchUserAndUserInfo(account);
        proxies = proxyService.searchProxy(user.getUid());
        if (proxies.size() > 0) {
            s = 1;
        } else {
            s = 0;
        }

        return SUCCESS;
    }
    
    /**
     * 查找代理商列表
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchProxy() {
        if (page <= 0) {
            page = 1;
        }
        int sortable=-1;
        if("pid".equals(sort)){
            sortable = ("asc".equals(sord))?IProxyService.ENTRANCE_SORTABLE_SN:IProxyService.ENTRANCE_SORTABLE_SN_DESC;
        }
        Map<String, Object> map = null;
        map = proxyService.searchProxyList(cid,account,proxyName,page,UserFinalPageSize.USERIC_ICARD_PAGESIZE,sortable);
        proxies = (List<Proxy>) map.get(ICommonDAO.OBJ_LIST);
        if (null == proxies) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = proxies.size();
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 检查用户名是否存在，读取用户信息
     * 
     * @return
     */
    public String searchProxyByUid() {
        user = adminUserService.searchUserAndUserInfo(account);
        // 帐号不存在
        if (null == user) {
            s = 0;
            return SUCCESS;
        }

        proxy = proxyService.searchProxyByUid(user.getUid());
        if (null != proxy) {
            s = 2;
            return SUCCESS;
        }
        
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 添加代理
     * 
     * @return
     */
    public String insertProxy() {
        if (null == uid) {
            // 新增用户
            User user = new User(account, MD5Util.getMD5("888888"), account, null);
            user.setPhone(phone);
            user.setEmail(email);
            user.setIsbaby((short) 1);
            userService.insertUserNormal(user);
            user = userService.searchUserByCondition(account);
            uid = user.getUid();
        }
        // 新增代理商
        s = proxyService.insertProxy(proxyName, uid, cidStr);
        
        
        return SUCCESS;
    }
    
    /**
     * 修改代理信息
     * 
     * @return
     */
    public String updateProxy() {
        proxyService.updateProxyName(uid, proxyName);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询代理商的代理区域
     * @return
     */
    public String searchProxyInfo() {
        proxy = proxyService.searchProxyByPid(pid);
        proxyc=new ArrayList<ProxyCity>();
        List<ProxyCity> lpc = proxyService.searchProxyCity(pid);
        if(null != lpc && lpc.size() > 0){ 
            for(ProxyCity tpc : lpc){
                tpc.setCname(CityUtil.addressDetail(tpc.getCid()));
            }
            proxyc.addAll(lpc);
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 保存代理商信息
     * @return
     */
    public String saveProxyInfo() {
        s = proxyService.updateProxyInfo(pid, email, phone, proxyName, cidStr);
        return SUCCESS;
    }
    
    /**
     * 删除代理商
     * @return
     */
    public String deleteProxy() {
        s = proxyService.deleteProxy(pid);
        return SUCCESS;
    }

    /*
     * public Map<Integer, String> getMap() { return map; }
     */
    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }
    
    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public void setAccount(String account) {
        this.account = account;
    }


    public int getS() {
        return s;
    }



    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public List<Proxy> getProxies() {
        return proxies;
    }

    public void setProxies(List<Proxy> proxies) {
        this.proxies = proxies;
    }

    public void setProxyService(IProxyService proxyService) {
        this.proxyService = proxyService;
    }

    public User getUser() {
        return user;
    }

    public List<ProxyCity> getProxyc() {
        return proxyc;
    }

    public Map<Integer, String> getCmap() {
        return cmap;
    }

    public String getProxyName() {
        return proxyName;
    }

    public void setProxyName(String proxyName) {
        this.proxyName = proxyName;
    }

    public Proxy getProxy() {
        return proxy;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }
    
    public Integer getPid() {
        return pid;
    }

    public void setCidStr(String cidStr) {
        this.cidStr = cidStr;
    }

    

}
